#include <bits/stdc++.h>
using namespace std;
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
using ll = long long;
const ll N = 1e5 + 10;
ll son[N][26], indx, cnt[N];
void insert(string s)
{
    int p = 0;
    for (ll i = 0; i < s.size(); i++)
    {
        ll now = s[i] - 'a';
        if (!son[p][now])
            son[p][now] = ++indx;
        p = son[p][now];
    }
    cnt[p]++;
}
int query(string s)
{
    int p = 0;
    for (ll i = 0; i < s.size();i++)
    {
        ll now = s[i] - 'a';

        if(!son[p][now])
            return 0;
        p = son[p][now];
    }
    return cnt[p];
}
void solve()
{
    ll n;
    cin >> n;
    string str;
    while (n--)
    {
        char c;
        cin >> c;

        cin >> str;
        if (c == 'I')
            insert(str);
        else
            cout << query(str) << endl;
    }
}
int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
